/**
 * Class List
 * This class create a list and add items from data array
 * Example:
 * var list = new List("list-id", "template-id");
 * list.setOnItemSelectedListener(function(е, index){
 *      //this -> clicked item
 *      //index -> index of clicked item
 * });
 *
 * list.setOnItemCreatedListener(function(e, item){
 *      //this -> list instance
 *      //item -> created item
 * });
 *
 * list.setItems(dataArray);
 * //add extra item with different template
 * list.addItem(data, "template-id", "li");
 */
List = (function(_super) {

    __extends(List, _super);
    /**
     * List constructor
     * @param view {View|String|HTMLElement} view, string id or html element
     * @param [template] {String} id of template
     * @param [itemtag] {String} html tag for item
     * @constructor
     */
    function List(view, template, itemtag) {
        List.__super__.constructor.call(this);
        this.setContentView(view || document.createElement("ul"));
		this.setTemplate(template);
		this.setItemTag(itemtag);
        this.items = [];
    }
    /**
     * Set html tag for list item
     * @param tag {String} html tag
     */
	List.prototype.setItemTag = function(tag){
		this.itemTag = tag;
	};
    /**
     * Set Template id
     * @param template {String} template id
     */
	List.prototype.setTemplate = function(template){
		this.templateID = template;
	};
    /**
     * Add one extra or external item
     * @param data {*} item data
     * @param [template] {String} template id
     * @param [tag] {String} html tag
     */
    List.prototype.addItem = function(data, template, tag){
        var item = new Item(data, template || this.templateID, tag || this.itemTag);
        if(this.listener) item.setOnClickListener(this.listener, this.items.length);
        this.items.push(item);
		if(this.itemCreatedListener) this.itemCreatedListener.call(this, item);
        this.view.append(item.view);
    };
    /**
     * Set items form array
     * @param data {Array} array of item data
     */
    List.prototype.setItems = function(data) {
        var _i, _data,
            _this = this;
        this.data = data;
		this.removeItems();
        for (_i = 0; _i < data.length; _i++) {
            _data = data[_i];
            _this.addItem(_data);
        }
    };
    /**
     * Remove items and html
     */
	List.prototype.removeItems = function(){
		this.items.length = 0;
		this.view.html("");
	};
    /**
     * Remove only one item by index
     * @param index {Number} item index
     */
	List.prototype.removeItem = function(index){
		var _item = this.items.splice(index, 1);
		_item.remove();
	};
    /**
     * Set On Item Created Listener
     * @param listener {Function} item created listener
     */
	List.prototype.setOnItemCreatedListener = function(listener){
        if(!(listener instanceof Function)) throw "listener is not a Function";
		this.itemCreatedListener = listener;
	};
    /**
     * Set on item selected listener
     * @param listener {Function} item selected listner
     */
    List.prototype.setOnItemSelectedListener = function(listener){
        if(!(listener instanceof Function)) throw "listener is not a Function";
        this.listener = listener;
    };

    return List;

})(Base);
